import sys
import pygame
from pygame.locals import *
import guanqia
import level
from game import Game, Level
import levelsMap


def xiayiguan():
    f = open("data/level.txt", "r")
    level_list = f.read()
    print(level_list)
    f.close()
    f = open("data/level.txt", "w")

    filename = 'data/levelGo.txt'
    # 使用'r'模式打开文件，读取内容

    if int(level_list) == 1:
        f.write("2")
        f.close()
        Level.levels = levelsMap.level2()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 1:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[1] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)

    if int(level_list) == 2:
        f.write("3")
        f.close()
        Level.levels = levelsMap.level3()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 2:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[2] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)

    if int(level_list) == 3:
        f.write("4")
        f.close()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 3:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[3] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)

        Level.levels = levelsMap.level4()

    if int(level_list) == 4:
        f.write("5")
        f.close()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 4:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[4] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)

        Level.levels = levelsMap.level5()

    if int(level_list) == 5:
        f.write("6")
        f.close()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 5:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[5] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)

        Level.levels = levelsMap.level6()

    if int(level_list) == 6:
        f.write("7")
        f.close()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 6:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[6] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)
        file.close()

        Level.levels = levelsMap.level7()

        Game().__init__()
        level.game_star()
    if int(level_list) == 7:
        f.write("8")
        f.close()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 7:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[7] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)
        file.close()

        Level.levels = levelsMap.level8()

        Game().__init__()
        level.game_star()
    if int(level_list) == 8:
        f.write("9")
        f.close()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 8:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[8] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)
        Level.levels = levelsMap.level9()

    if int(level_list) == 9:
        f.write("10")
        f.close()

        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
        # 检查文件至少有三行
        if len(lines) > 9:
            # 更改第三行文字
            new_line = "True\n"  # 这里输入您想要的新内容
            lines[9] = new_line
        # 使用'w'模式打开文件，写入内容
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(lines)
        Level.levels = levelsMap.level10()

    if int(level_list) == 10:
        f.write("10")
        f.close()
        Level.levels = levelsMap.level10()
    Game().__init__()
    level.game_star()


class Jiemian:
    def __init__(self):
        W = 1600
        H = 900
        pygame.init()

        pygame.display.set_caption("零零守护者")

        try:
            chuangkou = open("data/窗口大小.txt", "r", encoding="utf-8")
            chuangkou_size = chuangkou.read()
            chuangkou.close()
            print(chuangkou_size)
            if str(chuangkou_size) == "全屏模式":
                window = pygame.display.set_mode((W, H), FULLSCREEN | HWSURFACE, 32)
            else:
                window = pygame.display.set_mode((W, H))
        except:
            window = pygame.display.set_mode((W, H))

        img = pygame.image.load("img/界面背景.png")  # 如果你要尝试，要么图片放在原位，要么改变路径
        pygame.mixer.music.load("wav/胜利.wav")  # 替换为你的音乐文件路径
        pygame.mixer.music.play()  # -1表示无限循环播放

        # 按钮字体
        button_font = pygame.font.Font('font/datouren.ttf', 50)
        button_font2 = pygame.font.Font('font/datouren.ttf', 20)
        button_font3 = pygame.font.Font('font/datouren.ttf', 130)

        x5, y5 = 100, 100
        width5, height5 = 1400, 700
        button_rect5 = pygame.Rect(x5, y5, width5, height5)
        button_color5 = (200, 178, 150)
        text_color5 = (0, 0, 0)
        button_text5 = button_font3.render("恭喜您，成功过关！", True, text_color5)

        # 开始按钮
        x, y = 400, 590
        width, height = 200, 80
        button_rect = pygame.Rect(x, y, width, height)
        button_color = (100, 102, 178)
        text_color = (0, 0, 0)
        button_text = button_font.render("下一关", True, text_color)

        # 设置按钮
        x1, y1 = 700, 590
        width1, height1 = 200, 80
        button_rect1 = pygame.Rect(x1, y1, width1, height1)
        button_color1 = (100, 102, 178)
        text_color1 = (0, 0, 0)
        button_text1 = button_font.render("重玩", True, text_color1)

        # 设置按钮
        x2, y2 = 1000, 590
        width2, height2 = 200, 80
        button_rect2 = pygame.Rect(x2, y2, width2, height2)
        button_color2 = (100, 102, 178)
        text_color2 = (0, 0, 0)
        button_text2 = button_font.render("返回", True, text_color2)

        # 音量按钮
        x3, y3 = 0, 0
        width3, height3 = 50, 50
        button_rect3 = pygame.Rect(x3, y3, width3, height3)
        button_color3 = (100, 102, 78)
        text_color3 = (0, 0, 0)
        button_text3 = button_font2.render("静音", True, text_color3)

        # 加载按钮声音
        button_sound = pygame.mixer.Sound("wav/跳.wav")  # 替换为你的音乐文件路径

        click_button = 0
        # 定义按钮状态
        button_pressed = False
        button_pressed1 = False
        button_pressed2 = False
        button_pressed3 = False
        Fullscreen = False
        Game.flag = True
        Game.direction_D2 = False
        Game.direction_A2 = False
        while Game.flag:
            # 背景图刷新
            window.blit(img, (0, 1))
            # 绘制按钮
            # 开始按钮
            pygame.draw.rect(window, button_color5, button_rect5)
            window.blit(button_text5, (button_rect5.centerx - button_text5.get_width()
                                       / 2, button_rect5.centery - button_text5.get_height() / 2 - 100))

            pygame.draw.rect(window, button_color, button_rect)
            window.blit(button_text, (button_rect.centerx - button_text.get_width()
                                      / 2, button_rect.centery - button_text.get_height() / 2))

            # 设置按钮
            pygame.draw.rect(window, button_color1, button_rect1)
            window.blit(button_text1, (button_rect1.centerx - button_text1.get_width()
                                       / 2, button_rect1.centery - button_text1.get_height() / 2))
            # 退出按钮
            pygame.draw.rect(window, button_color2, button_rect2)
            window.blit(button_text2, (button_rect2.centerx - button_text2.get_width()
                                       / 2, button_rect2.centery - button_text2.get_height() / 2))

            # 音量按钮
            pygame.draw.rect(window, button_color3, button_rect3)
            window.blit(button_text3, (button_rect3.centerx - button_text3.get_width()
                                       / 2, button_rect3.centery - button_text3.get_height() / 2))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    Game.flag = False
                    sys.exit()

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        Fullscreen = not Fullscreen
                        if Fullscreen:
                            window = pygame.display.set_mode((W, H), FULLSCREEN | HWSURFACE, 32)
                        else:
                            window = pygame.display.set_mode((W, H), 0, 32)
                    if event.key == pygame.K_SPACE:
                        xiayiguan()

                # 点击按钮
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("下一关按钮")
                        button_color = (255, 255, 0)
                        button_pressed = True
                        button_sound.play()
                        xiayiguan()

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect1.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("重开按钮")
                        button_color1 = (255, 255, 0)
                        button_pressed1 = True
                        button_sound.play()
                        f = open("data/level.txt", "r")
                        level_list = f.read()
                        print(level_list)
                        f.close()
                        f = open("data/level.txt", "w")

                        filename = 'data/levelGo.txt'
                        # 使用'r'模式打开文件，读取内容

                        if int(level_list) == 1:
                            f.write("1")
                            f.close()
                            Level.levels = levelsMap.level1()

                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 0:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[0] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)

                        if int(level_list) == 2:
                            f.write("2")
                            f.close()
                            Level.levels = levelsMap.level2()

                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 1:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[1] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)

                        if int(level_list) == 3:
                            f.write("3")
                            f.close()

                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 2:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[2] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)

                            Level.levels = levelsMap.level3()

                        if int(level_list) == 4:
                            f.write("4")
                            f.close()

                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 3:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[3] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)
                            Level.levels = levelsMap.level4()

                        if int(level_list) == 5:
                            f.write("5")
                            f.close()
                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 4:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[4] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)
                            Level.levels = levelsMap.level5()

                        if int(level_list) == 6:
                            f.write("6")
                            f.close()

                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 5:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[5] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)
                            Level.levels = levelsMap.level6()

                        if int(level_list) == 7:
                            f.write("7")
                            f.close()

                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 6:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[6] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)
                            Level.levels = levelsMap.level7()

                        if int(level_list) == 8:
                            f.write("8")
                            f.close()

                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 7:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[7] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)
                            Level.levels = levelsMap.level8()

                        if int(level_list) == 9:
                            f.write("9")
                            f.close()
                            with open(filename, 'r', encoding='utf-8') as file:
                                lines = file.readlines()
                            # 检查文件至少有三行
                            if len(lines) > 8:
                                # 更改第三行文字
                                new_line = "True\n"  # 这里输入您想要的新内容
                                lines[8] = new_line
                            # 使用'w'模式打开文件，写入内容
                            with open(filename, 'w', encoding='utf-8') as file:
                                file.writelines(lines)
                            Level.levels = levelsMap.level9()

                        if int(level_list) == 10:
                            f.write("10")
                            f.close()
                            Level.levels = levelsMap.level10()

                        file.close()
                        Game().__init__()
                        level.game_star()

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect2.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("返回按钮")
                        button_color2 = (255, 255, 0)
                        button_pressed2 = True
                        button_sound.play()
                        guanqia.Jiemian()

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect3.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("音量按钮")
                        button_color3 = (255, 255, 0)
                        click_button += 1
                        if click_button % 2 == 1:
                            button_text3 = button_font2.render("播放", True, text_color3)
                            # 暂停音乐
                            pygame.mixer.music.pause()
                        if click_button % 2 == 0:
                            button_text3 = button_font2.render("暂停", True, text_color3)
                            pygame.mixer.music.unpause()
                        button_pressed3 = True

                # 处理鼠标松开事件
                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed:
                        button_color = (45, 102, 78)
                        button_pressed = False
                        print("开始按钮松开了")

                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed1:
                        button_color1 = (45, 102, 78)
                        button_pressed1 = False
                        print("设置按钮")

                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed2:
                        button_color2 = (45, 102, 78)
                        button_pressed2 = False

                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed3:
                        button_color3 = (45, 102, 78)
                        button_pressed3 = False
                        print("音量松开了")

            pygame.display.flip()  # 更新屏幕显示


if __name__ == '__main__':
    Jiemian()
